/*
 * Copyright (c) 2022 EKA2L1 Team
 *
 * This file is part of EKA2L1 project.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

package com.github.eka2l1.settings;

import com.github.eka2l1.emu.Emulator;

import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;
import org.yaml.snakeyaml.nodes.Tag;
import org.yaml.snakeyaml.representer.Representer;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

public class BTNetplayDirectAddressesManager {

    static ArrayList<BTNetplayAddress> getAddresses() {
        ArrayList<BTNetplayAddress> addresses = new ArrayList<>();
        Yaml yaml = new Yaml();
        AppDataStore dataStore = AppDataStore.getEmulatorStore();
        ArrayList<?> list = dataStore.getArrayList("internet-bluetooth-friends", null);
        if (list != null) {
            for (Object obj : list) {
                String str = yaml.dump(obj);
                BTNetplayAddress address = yaml.loadAs(str, BTNetplayAddress.class);
                addresses.add(address);
            }
        }
        return addresses;
    }

    static void saveAddresses(ArrayList<BTNetplayAddress> addresses) {
        AppDataStore dataStore = AppDataStore.getEmulatorStore();
        dataStore.putArrayList("internet-bluetooth-friends", addresses);
        dataStore.save();
    }
}
